//
//  LSMatchExponentCell.m
//  LookScore
//
//  Created by wr on 2017/12/11.
//  Copyright © 2017年 ChongQingXunTiCompany. All rights reserved.
//

#define controlWidth ((LSScreenWidth -LSSpaceLeftWidth1)*0.6f)/3

#import "LSMatchExponentCell.h"
#import "LSExponentModel.h"
#import "LSAIHistoricaModel.h"
#import "LSExponentCompanySponsorCell.h"
#import "LSCellExponentCell.h"
#import "LSTypeButton.h"

@interface LSMatchExponentCell()<UITableViewDelegate,UITableViewDataSource,LSTypeButtonActionDelegate>

@property(nonatomic,strong)UITableView *tableView;

@property (nonatomic, strong) UIView *topView;

@property (nonatomic, strong) UIView *bottomView;

@property (nonatomic, strong) UILabel *leagueNameLable;

//@property (nonatomic, strong) UILabel *gameInfoLable;
@property (nonatomic, strong) UILabel *homeTeamOrder;

@property (nonatomic, strong) UILabel *homeTeamName;

@property (nonatomic, strong) UILabel *vsLable;

@property (nonatomic, strong) UILabel *guestTeamOrder;

@property (nonatomic, strong) UILabel *guestTeamName;

@property (nonatomic, strong) UILabel *timeLable;

@property (nonatomic, strong) UIImageView *arrowImg;

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation LSMatchExponentCell{
    NSInteger typeIndex;
}

- (UIView *)topView{
    if (!_topView) {
        _topView = [[UIView alloc] init];
        _topView.backgroundColor = LSTitleWhiteColor;
    }
    return _topView;
}
- (UIView *)bottomView{
    if (!_bottomView) {
        _bottomView = [[UIView alloc] init];
        _bottomView.hidden = YES;
        _bottomView.backgroundColor = LSSubGrayColorf8;
//        _bottomView.backgroundColor = [UIColor redColor];
    }
    return _bottomView;
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor =LSSubGrayColorf7;
        _tableView.scrollEnabled = NO;
        //        _tableView.sectionHeaderHeight=0;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
+(LSMatchExponentCell *)MatchExponentTableviewCellByTableView:(UITableView *)tableView{
    LSMatchExponentCell *cell=[tableView dequeueReusableCellWithIdentifier:NSStringFromClass(self)];
    if (!cell) {
        cell=[[LSMatchExponentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass(self)];
//        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    return cell;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
//        typeIndex = 0;
        [self addContentView];
    }
    
    return self;
}

-(void)addContentView{
    
    self.contentView.backgroundColor = LSSubGrayColorf7;
    
    
    [self.contentView addSubview:self.topView];
    self.topView.sd_layout.topEqualToView(self.contentView).leftEqualToView(self.contentView).rightEqualToView(self.contentView).heightIs(47);
    
    self.leagueNameLable = [[UILabel alloc] init];
//    self.leagueNameLable.text = @"中超";
    self.leagueNameLable.textColor = LSTitleWhiteColor;
    self.leagueNameLable.textAlignment = NSTextAlignmentCenter;
    self.leagueNameLable.backgroundColor = LSNavMainColor;
    self.leagueNameLable.font = LSFontSize11;
    [self.topView addSubview:self.leagueNameLable];
    self.leagueNameLable.sd_layout.leftEqualToView(self.topView).offset(LSAutoVlaue(10, LSScreenWidth)).centerYEqualToView(self.topView).widthIs(LSAutoVlaue(50, LSScreenWidth)).autoHeightRatio(0);
    
    self.arrowImg = [[UIImageView alloc] init];
    self.arrowImg.image = [UIImage imageNamed:@"MineRightLightGray"];
    [self.contentView addSubview:self.arrowImg];
    self.arrowImg.sd_layout.centerYEqualToView(self.topView).rightEqualToView(self.topView).offset(-LSAutoVlaue(5, LSScreenWidth)).widthIs(LSAutoVlaue(7, LSScreenWidth)).heightIs(LSAutoVlaue(15, LSScreenHeight));
    
    self.timeLable = [[UILabel alloc] init];
    self.timeLable.textAlignment = NSTextAlignmentCenter;
    self.timeLable.textColor = LSTitleGrayColor3;
    self.timeLable.font = LSFontSize(11,LSScreenWidth);
    [self.topView addSubview:self.timeLable];
    self.timeLable.sd_layout.rightSpaceToView(self.arrowImg, 0).centerYEqualToView(self.topView).widthIs(LSAutoVlaue(70, LSScreenWidth)).autoHeightRatio(0);
    
    self.vsLable = [[UILabel alloc] init];
    self.vsLable.text = @"VS";
    self.vsLable.textAlignment = NSTextAlignmentCenter;
    self.vsLable.textColor = LSSubColorf25e59;
    self.vsLable.font = LSFontSize(11,LSScreenWidth);
    [self.topView addSubview:self.vsLable];
    self.vsLable.sd_layout.centerXEqualToView(self.topView).offset(-LSAutoVlaue(10, LSScreenWidth)).centerYEqualToView(self.topView).autoHeightRatio(0).widthIs(LSAutoVlaue(15, LSScreenWidth));
    
    self.homeTeamOrder = [[UILabel alloc] init];
    self.homeTeamOrder.textAlignment = NSTextAlignmentCenter;
    self.homeTeamOrder.textColor = LSTitleGrayColor3;
    self.homeTeamOrder.font = LSFontSize(11,LSScreenWidth);
    [self.topView addSubview:self.homeTeamOrder];
    self.homeTeamOrder.sd_layout
    .leftSpaceToView(self.leagueNameLable, LSAutoVlaue(10, LSScreenWidth))
    .centerYEqualToView(self.topView)
    .autoHeightRatio(0)
    .widthIs(LSAutoVlaue(25, LSScreenWidth));
    
    self.guestTeamOrder = [[UILabel alloc] init];
    self.guestTeamOrder.textAlignment = NSTextAlignmentCenter;
    self.guestTeamOrder.textColor = LSTitleGrayColor3;
    self.guestTeamOrder.font = LSFontSize(11,LSScreenWidth);
    [self.topView addSubview:self.guestTeamOrder];
    self.guestTeamOrder.sd_layout
    .rightSpaceToView(self.timeLable, LSAutoVlaue(10, LSScreenWidth))
    .centerYEqualToView(self.topView)
    .autoHeightRatio(0)
    .widthIs(LSAutoVlaue(25, LSScreenWidth));
    
    self.homeTeamName = [[UILabel alloc] init];
    self.homeTeamName.textAlignment = NSTextAlignmentCenter;
    self.homeTeamName.numberOfLines = 2;
    self.homeTeamName.textColor = LSSubColor5D5D5D;
    self.homeTeamName.font = LSFontSize(13,LSScreenWidth);
    [self.topView addSubview:self.homeTeamName];
    self.homeTeamName.sd_layout
    .leftSpaceToView(self.homeTeamOrder,0)
    .rightSpaceToView(self.vsLable,LSAutoVlaue(10, LSScreenWidth))
    .centerYEqualToView(self.topView)
    .heightIs(self.contentView.height);
    
    self.guestTeamName = [[UILabel alloc] init];
    self.guestTeamName.textAlignment = NSTextAlignmentCenter;
    self.guestTeamName.numberOfLines = 2;
    self.guestTeamName.textColor = LSSubColor5D5D5D;
    self.guestTeamName.font = LSFontSize(13,LSScreenWidth);
    [self.topView addSubview:self.guestTeamName];
    self.guestTeamName.sd_layout
    .leftSpaceToView(self.vsLable, LSAutoVlaue(10, LSScreenWidth))
    .rightSpaceToView(self.guestTeamOrder, 0)
    .centerYEqualToView(self.topView)
    .heightIs(self.contentView.height);
    
    [self.contentView addSubview:self.bottomView];
    self.bottomView.sd_layout.topSpaceToView(self.topView, LSAutoVlaue(8, LSScreenHeight                                                                                            )).leftEqualToView(self.contentView).offset(LSAutoVlaue(10, LSScreenWidth)).rightEqualToView(self.contentView).offset(-LSAutoVlaue(10, LSScreenWidth)).bottomEqualToView(self.contentView).offset(-LSAutoVlaue(10, LSScreenHeight));
    
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.bottomView addSubview:self.tableView]; self.tableView.sd_layout.leftEqualToView(self.bottomView).offset(LSAutoVlaue(5, LSScreenWidth)).rightEqualToView(self.bottomView).offset(-LSAutoVlaue(5, LSScreenWidth)).topEqualToView(self.bottomView).offset(LSAutoVlaue(10, LSScreenHeight)).bottomEqualToView(self.bottomView).offset(-LSAutoVlaue(25, LSScreenHeight));
    
    UIButton *checkMoreBtn = [[UIButton alloc] init];
    checkMoreBtn.backgroundColor = [UIColor clearColor];
    [checkMoreBtn setTitle:@"查看详情" forState:UIControlStateNormal];
    [checkMoreBtn setTitleColor:LSSubGrayColorf9 forState:UIControlStateNormal];
    checkMoreBtn.titleLabel.font = LSFontSize10;
    [checkMoreBtn addTarget:self action:@selector(clickCheckMoreBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.bottomView addSubview:checkMoreBtn];
    checkMoreBtn.sd_layout.leftEqualToView(self.tableView).topSpaceToView(self.tableView, 0).rightEqualToView(self.tableView).bottomEqualToView(self.bottomView);
}


- (void)setModel:(LSExponentList *)model{
    _model = model;
    if (_model.isOpen) {
        self.bottomView.hidden = NO;
        self.arrowImg.hidden = YES;
        self.topView.backgroundColor = self.contentView.backgroundColor;
    }
    else{
        self.bottomView.hidden = YES;
        self.arrowImg.hidden = NO;
        self.topView.backgroundColor = LSTitleWhiteColor;
    }
    
    self.leagueNameLable.text = model.LeagueName;
    self.leagueNameLable.backgroundColor = [CommonClass colorWithHexString:model.LeagueColor];
//    self.homeTeamName.text = model.HomeTeamName;
//    self.guestTeamName.text = model.GuestTeamName;

    
    if ([CommonClass isBlankString:model.HomeTeamOrder]) {
        self.homeTeamOrder.text=@"";
    }else{
        NSCharacterSet* nonDigits1 =[[NSCharacterSet decimalDigitCharacterSet] invertedSet];
        NSString *homeTeamOrder =[model.HomeTeamOrder stringByTrimmingCharactersInSet:nonDigits1];
        self.homeTeamOrder.text = [NSString stringWithFormat:@"[%@]",homeTeamOrder];
    }
    
    if ([CommonClass isBlankString:model.GuestTeamOrder]) {
        
        self.guestTeamOrder.text=@"";
    }else{
        
        NSCharacterSet* nonDigits2 =[[NSCharacterSet decimalDigitCharacterSet] invertedSet];
        NSString *guestTeamOrder =[model.GuestTeamOrder stringByTrimmingCharactersInSet:nonDigits2];
        self.guestTeamOrder.text = [NSString stringWithFormat:@"[%@]",guestTeamOrder];
    }
    
    
    self.timeLable.text = [CommonClass timestampToString:model.TimeStamp];
    
    NSMutableAttributedString * host  = [[NSMutableAttributedString alloc] initWithData:[model.HomeTeamName dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
    NSMutableParagraphStyle *muParagraph = [[NSMutableParagraphStyle alloc]init];
    muParagraph.alignment = NSTextAlignmentCenter;
    [host setAttributes:@{NSParagraphStyleAttributeName : muParagraph,NSFontAttributeName:LSFontSize(13, LSScreenWidth),NSForegroundColorAttributeName : LSSubColor5D5D5D} range:NSMakeRange(0, host.length)];
    self.homeTeamName.attributedText = host;
    
    NSMutableAttributedString * guest = [[NSMutableAttributedString alloc] initWithData:[model.GuestTeamName dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
    
    [guest setAttributes:@{NSParagraphStyleAttributeName : muParagraph,NSFontAttributeName:LSFontSize(13, LSScreenWidth),NSForegroundColorAttributeName:LSSubColor5D5D5D} range:NSMakeRange(0, guest.length)];
    self.guestTeamName.attributedText = guest;
    
    [self.tableView reloadData];

}
#pragma mark - TypeButtonDelegate
//实现代理
-(void)didClickTypeButtonAction:(UIButton*)button withIndex:(NSInteger)index
{
    MyLog(@"现在点击的是%zd个",index);
    typeIndex = index;
    
    [self.tableView reloadData];
}

#pragma mark 🔥 ButtonTarget
- (void)clickCheckMoreBtn:(UIButton *)sender{
    if (self.ClickCheckMoreBtnBlock) {
        self.ClickCheckMoreBtnBlock();
    }
}

#pragma mark -- UITableViewDelegate & dataSource
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(10, 0, LSScreenWidth-20, 70)];
    header.backgroundColor = self.bottomView.backgroundColor;
    
    LSTypeButton  *typeBtnView = [[LSTypeButton  alloc]initWithFrame:CGRectMake(0, 0, LSScreenWidth -20, 38)];
    [typeBtnView setTypeButtonTitles:@[@"欧赔",@"亚盘",@"大小球"] withDownLableHeight:2 andDeleagte:self];
    [typeBtnView setSelectButtonIndex:typeIndex];
    [typeBtnView setTypeButtonNormalColor:LSTitleGrayColor4 andSelectColor:LSNavMainColor];
    typeBtnView.backgroundColor = header.backgroundColor;
    [header addSubview:typeBtnView];
    
    UIView *titleView = [[UIView alloc] init];
    titleView.backgroundColor = LSTitleWhiteColor;
    [header addSubview:titleView];
    titleView.sd_layout.bottomEqualToView(header).offset(-1).leftEqualToView(header).rightEqualToView(header).topSpaceToView(typeBtnView, 0);
    NSArray *titleArray;
    switch (typeIndex) {
        case 0:
            titleArray = @[@"公司",@"初盘/即时",@"胜",@"平",@"负"];
            break;
        case 1:{
            titleArray = @[@"公司",@"初盘/即时",@"主队",@"盘口",@"客队"];
        }
            break;
        case 2:{
            titleArray = @[@"公司",@"初盘/即时",@"大球",@"盘口",@"小球"];
        }
            break;
        default:
            break;
    }
//    NSArray *titleArray = @[@"公司",@"即时/初盘",@"主队",@"让球",@"客队"];
    CGFloat width=self.tableView.width/5;
    UILabel *lable;
    for (int i=0; i<5; i++) {
        lable=[UILabel new];
        lable.text = titleArray[i];
        lable.textColor = LSSubColor5D5D5D;
        lable.font=LSFontSize12;
        lable.textAlignment = NSTextAlignmentCenter;
        [titleView addSubview:lable];
        lable.sd_layout.leftEqualToView(titleView).offset(width*i).centerYEqualToView(titleView).widthIs(width).heightIs(30);
    }
    return header;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 70;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  
    if (typeIndex == 0) {
        return _model.OddsChangeRecordList.count;
    }else if (typeIndex == 1){
        return _model.AIHistoricaDataList.count;
    }
    return _model.SDHistoricaDataList.count;

    
    
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath   {
    
    LSCellExponentCell *cell = [LSCellExponentCell CellExponentTableviewCellByTableView:tableView];
    if (typeIndex == 0) {
        cell.model1 = _model.OddsChangeRecordList[indexPath.row];
    }else if (typeIndex == 1 ){
        cell.model2 = _model.AIHistoricaDataList[indexPath.row];
    }else{
        cell.model3 = _model.SDHistoricaDataList[indexPath.row];
    }
    return cell;
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 37;
}



@end
